//#include <avr/io.h>
//#include <avr/interrupt.h>

// this program switches between ADC pins on ATMega324
// still need to program how to compare to threshold value
// still need to program how to save 0 and/or 1 to array
// Cassi Latino 2/27/2012

#define IR_Threshold 240
#define Ultra_Threshold 64

void init_adc(void) {
	
	ADCSRA |= 1 << ADPS2;			//bit to set for micro-controller for ADC at 1MHz
	ADMUX  |= 1 << REFS0 | 1 << REFS1;	//enable internal voltage reference
	ADCSRA |= 1 << ADEN;			//enable ADC

	ADCSRA |= 1 << ADSC;			// establish single conversion
}

char read_sensor(char sensor)
{
	unsigned int adcValue, theLow;

	ADCSRA |= 1 << ADSC;	// establish single conversion

	ADMUX &= ~0xF;
	ADMUX |= sensor - 1;         


	while(ADSC==1);		// wait for ADC to finish
	theLow = ADCL;
	adcValue = ADCH<<8 | theLow;

	ADCSRA |= 1<<ADSC;	// establish single conversion

	switch (sensor)
	{
		case 1:
		case 3:
			if(adcValue >= IR_Threshold) return 1;
			break;
		case 2:
			if(adcValue <= Ultra_Threshold) return 1;
			break;
	}

	return 0;
}
